globalThis   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
eloc 3
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
1
/*!
2
 * Copyright (c) 2022 Pedro José Batista, licensed under the MIT License.
3
 * See the LICENSE.md file in the project root for more information.
4
 */
5
/* eslint-disable @typescript-eslint/naming-convention,@typescript-eslint/no-unsafe-argument */
6 1
import Decimal from "decimal.js";
7 1
import Format from "./format";
8
import type { Locale, FormatOptions, Notation, Style } from "./format";
9
10 1
const main = (Decimal: Decimal.Constructor) => {
11
    // Do not attempt to redefine the module, if already extended
12 2
    if (typeof Decimal.Format !== "undefined") {
13
        return;
14
    }
15
16 1
    Object.defineProperty(Decimal, "Format", { value: Format });
17 1
    Object.defineProperty(Decimal.prototype, "toLocaleString", {
18
        value: function (this: Decimal.Instance, locales, options) {
19 4
            return new Decimal.Format(locales, options).format(this);
20
        } as typeof Decimal.prototype.toLocaleString,
21
    });
22
};
23
24 2
main(globalThis.__Decimal__Class__Global__ ?? require("decimal.js"));
25
26
declare module "decimal.js" {
27
    export interface Decimal {
28
        /**
29
         * Returns a string with a language-sensitive representation of this decimal number.
30
         *
31
         * @template N Numeric notation of formatting.
32
         * @template S Numeric style of formatting.
33
         * @param locales A string with a [BCP 47](https://www.rfc-editor.org/info/bcp47) language tag, or an
34
         *   array of such strings.
35
         *
36
         *   For the general form and interpretation of this parameter, see the [Intl page on
37
         *   MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl).
38
         * @param options Object used to configure the behavior of the string localization.
39
         * @returns A localized and formatted string.
40
         */
41
        toLocaleString: <N extends Notation = "standard", S extends Style = "decimal">(
42
            locales?: Locale | Locale[],
43
            options?: FormatOptions<N, S>,
44
        ) => string;
45
    }
46
47
    // In order to appropriately represent the Format class, it needs to be placed on Decimal as a 'static' member:
48
49
    // eslint-disable-next-line @typescript-eslint/no-namespace
50
    export namespace Decimal {
51
        /**
52
         * The `Decimal.Format` object enables language-sensitive decimal number formatting. It is entirely
53
         * based on `Intl.NumberFormat`, with the options of the latter being 100% compatible with it.
54
         *
55
         * This class, however, extend the numeric digits constraints of `Intl.NumberFormat` from 21 to
56
         * 1000000000 in order to fully take advantage of the arbitrary-precision of `decimal.js`.
57
         */
58
        export { Format };
59
    }
60
}
61
62
declare global {
63
    export class globalThis {
64
        /** Used by the `extend` submodule to prevent it from loading directly from `decimal.js`. */
65
        static __Decimal__Class__Global__: Decimal.Constructor | undefined;
66
    }
67
}
68 1
export { Decimal };
69
export default Decimal;
70